/*
 * Copyright (c) 2010. Dylan Schell.
 */

package nl.xs4all.dylanschell.deckprinter.pages;

import com.google.inject.Inject;
import nl.xs4all.dylanschell.deckprinter.model.CardInDeck;
import nl.xs4all.dylanschell.deckprinter.model.CardInSet;
import nl.xs4all.dylanschell.deckprinter.model.DeckDescriptor;
import nl.xs4all.dylanschell.deckprinter.services.SetService;
import org.apache.wicket.Application;
import org.apache.wicket.Page;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.value.ValueMap;

import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: dylan
 * Date: Nov 22, 2010
 * Time: 6:27:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class DeckPreviewPage extends BasePage
{
    private final DeckDescriptor deck;

    @Inject
    public SetService setService;

    public DeckPreviewPage(final DeckDescriptor deck, final Page returnPage)
    {
        this.deck = deck;

        add(new BookmarkablePageLink("home", getApplication().getHomePage()));
        Link returnLink = new Link("returnLink")
        {
            @Override
            public void onClick()
            {
                setResponsePage(returnPage);
            }
        };
        add(returnLink);
        GridView<CardInDeck> gridView = new GridView<CardInDeck>("gridView", new IDataProvider<CardInDeck>()
        {
            public Iterator<? extends CardInDeck> iterator(int first, int count)
            {
                return deck.getMain().iterator();
            }

            public int size()
            {
                return deck == null ? 0 : deck.getMain().size();
            }

            public IModel<CardInDeck> model(CardInDeck object)
            {
                return Model.of(object);
            }

            public void detach()
            {
            }
        })
        {
            @Override
            protected void populateEmptyItem(Item<CardInDeck> cardInDeckItem)
            {
                cardInDeckItem.add(new WebMarkupContainer("image"));
            }

            @Override
            protected void populateItem(Item<CardInDeck> cardInDeckItem)
            {
                CardInDeck cid = cardInDeckItem.getModelObject();
                ResourceReference cardImage = new ResourceReference(Application.class, "card-images");
                CardInSet cis = setService.getCardInSet(cid);
                Image image = new Image("image", cardImage, new ValueMap("set=" + cis.getSetId() + ",card=" + cis.getId()));
                cardInDeckItem.add(image);
            }
        };
        gridView.setColumns(4);
        WebMarkupContainer gridViewContainer = new WebMarkupContainer("gridViewContainer");
        gridViewContainer.add(gridView);
        add(gridViewContainer);
        gridViewContainer.setOutputMarkupId(true);

    }

    @Override
    public String getTitle()
    {
        return "Preview";
    }

    private class CardInDeckDataProvider implements IDataProvider<CardInDeck>
    {
        private final String section;

        public CardInDeckDataProvider(String section)
        {
            this.section = section;
        }

        public Iterator<? extends CardInDeck> iterator(int first, int count)
        {
            return deck.getCardsInSection(section).iterator();
        }

        public int size()
        {
            return deck == null ? 0 : deck.getCardsInSection(section).size();
        }

        public IModel<CardInDeck> model(CardInDeck object)
        {
            return Model.of(object);
        }

        public void detach()
        {
        }
    }
}
